package com.example.innospace.demos.web.controller;

import com.example.innospace.demos.pojo.Request.ApprovalRequest;
import com.example.innospace.demos.pojo.enums.MessageEnum;
import com.example.innospace.demos.pojo.enums.ResponseCodeEnum;
import com.example.innospace.demos.pojo.result.R;
import com.example.innospace.demos.web.service.ApprovalRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 申请通知
 */
// 定义控制器类，处理与审核申请相关的 HTTP 请求
@RestController
@RequestMapping("/approval")
public class ApprovalRequestController {

    // 注入 ApprovalRequestService 用于调用业务逻辑
    @Autowired
    private ApprovalRequestService approvalRequestService;


    /**
     *提交申请
     * @param request
     * @return
     */
    @PostMapping("/submit")
    public R<ApprovalRequest> submitRequest(@RequestBody ApprovalRequest request) {
        return R.success(ResponseCodeEnum.SUCCESS,approvalRequestService.submitRequest(request), MessageEnum.OPERATION_SUCCESS);
    }

    /**
     * 处理获取待审批申请列表的 GET 请求
     * @return
     */
    @GetMapping("/pending")
    public R<List<ApprovalRequest>> getPendingRequests() {
        // 调用服务层的获取待审批申请列表方法
        return R.success(ResponseCodeEnum.SUCCESS,approvalRequestService.getPendingRequests(), MessageEnum.OPERATION_SUCCESS);
    }


    /**
     * 处理审核申请的 PUT 请求
     * @param requestId 申请请求
     * @param status 需要更新的状态PENDING/APPROVED/REJECTED 待定/同意/拒接
     * @return
     * 如果请求被同意，那么处理结果是按申请中存储的内容来更新的，但是如果被拒接则按被拒接的来修改
     */
    @PutMapping("/approve")
    public R approveRequest(@RequestParam String status,@RequestParam Long requestId) {
        // 调用服务层的审核申请方法
        approvalRequestService.approveRequest(status,requestId);
        return R.success(ResponseCodeEnum.SUCCESS,MessageEnum.OPERATION_SUCCESS);

    }
}
